package com.sywb.chuangyebao.view.dialog;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sywb.chuangyebao.R;

import org.bining.footstone.utils.ToastUtils;

import java.io.Serializable;

import butterknife.BindView;

public class BusinessTVDetailIntroDialog extends BaseDialogFragment {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.wv_content)
    WebView wvContent;

    private int contentHeight; // DialogActivity高度
    private String intro;

    public static BusinessTVDetailIntroDialog newInstance(Object... pramars) {
        BusinessTVDetailIntroDialog dialogFragment = new BusinessTVDetailIntroDialog();
        Bundle bundle = new Bundle();
        if (pramars != null && pramars.length > 0) {
            for (int i = 0; i < pramars.length; i++) {
                bundle.putSerializable("p" + i, (Serializable) pramars[i]);
            }
        }
        dialogFragment.setArguments(bundle);
        return dialogFragment;
    }

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.layout_business_detail_intro;
    }

    @Override
    public void initView(View view, Bundle bundle) {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                exit();
            }
        });

        if (bundle != null) {
            contentHeight = bundle.getInt("p0", 0);
            intro = bundle.getString("p1");
        } else {
            contentHeight = getArguments().getInt("p0", 0);
            intro = getArguments().getString("p1");
        }

        tvTitle.setText("简介");
        setHtml(intro);
    }

    @Override
    public void onResume() {
        super.onResume();
        setWindowHeight();
    }

    private void setWindowHeight() {
        WindowManager.LayoutParams p = getDialog().getWindow().getAttributes();
        p.width = RelativeLayout.LayoutParams.MATCH_PARENT;
        if (contentHeight > 0) {
            p.height = contentHeight;
        } else {
            p.height = RelativeLayout.LayoutParams.MATCH_PARENT;
        }
        // p.gravity = Gravity.BOTTOM;
        // p.dimAmount = 0.8f;
        getDialog().getWindow().setAttributes(p);
    }

    /**
     * 初始化WebView
     */
    @SuppressLint("SetJavaScriptEnabled")
    private void initWebView() {
        /**
         * setAllowFileAccess 启用或禁止WebView访问文件数据 setBlockNetworkImage 是否显示网络图像
         * setBuiltInZoomControls 设置是否支持缩放 setCacheMode 设置缓冲的模式
         * setDefaultFontSize 设置默认的字体大小 setDefaultTextEncodingName 设置在解码时使用的默认编码
         * setFixedFontFamily 设置固定使用的字体 setJavaSciptEnabled 设置是否支持Javascript
         * setLayoutAlgorithm 设置布局方式 setLightTouchEnabled 设置用鼠标激活被选项
         * setSupportZoom 设置是否支持变焦
         * */
        WebSettings settings = wvContent.getSettings();
        settings.setJavaScriptEnabled(true);// 设置支持javascript脚本
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        // 1.NARROW_COLUMNS：可能的话使所有列的宽度不超过屏幕宽度；2.NORMAL：正常显示不做任何渲染；3.SINGLE_COLUMN：把所有内容放大webview等宽的一列中
        // settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);// 排版适应屏幕
        settings.setDisplayZoomControls(false);
        settings.setAllowFileAccess(true); // 启用或禁止WebView访问文件数据
        settings.setBuiltInZoomControls(false); //  隐藏缩放按钮
        settings.setSupportZoom(true); // 支持缩放
        // settings.setUseWideViewPort(true);// 可任意比例缩放
        settings.setLoadWithOverviewMode(true);//setUseWideViewPort方法设置webview推荐使用的窗口；设置webview加载的页面的模式
        settings.setGeolocationEnabled(true);// 启用地理定位
        settings.setDomStorageEnabled(true);
        settings.setSaveFormData(true);// 保存表单数据
        // settings.setSavePassword(true);
        // settings.setTextZoom(200);
        // settings.setMixedContentMode(1);
        // settings.setAcceptThirdPartyCookies();
        wvContent.setWebChromeClient(new WebChromeClient());
        wvContent.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }
        });
    }

    /**
     * 设置需展示的Html片段
     */
    private void setHtml(String protocol) {
        if (protocol != null) {
            wvContent.getSettings().setDefaultTextEncodingName("utf-8");
            wvContent.loadDataWithBaseURL("http://www.3158.cn", protocol, "text/html", "UTF-8", null);
        } else {
            ToastUtils.show(mActivity, "未获取到内容，请稍后重试");
        }
        hideProgress();
    }

}
